package com.woniuxy.yoga.admin.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.admin.dao.model.Resource;
import com.woniuxy.yoga.admin.model.form.menu.CancelRelationForm;
import com.woniuxy.yoga.admin.model.form.resource.AddResourceForm;
import com.woniuxy.yoga.admin.model.form.resource.CancelResourceRelationForm;
import com.woniuxy.yoga.admin.model.form.resource.ReResourceRelationForm;
import com.woniuxy.yoga.admin.model.form.resource.SelectResourcePageForm;
import com.woniuxy.yoga.admin.model.param.menu.CancelRelationParam;
import com.woniuxy.yoga.admin.model.param.resource.AddResourceParam;
import com.woniuxy.yoga.admin.model.param.resource.CancelResourceRelationParam;
import com.woniuxy.yoga.admin.model.param.resource.ReResourceRelationParam;
import com.woniuxy.yoga.admin.model.param.resource.SelectResourcePageParam;
import com.woniuxy.yoga.admin.permission.Permission;
import com.woniuxy.yoga.admin.service.ResourceService;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源相关接口")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;
    /**
     * 通过资源ID集合查询资源集合
     * @param resourceIds
     * @return
     */
    @PostMapping("/selectResourceByIds")
    @ApiOperation("查询资源集合通过资源ID集合")
    @YogaLog("查询资源集合通过资源ID集合")
    public Result<List<Resource>> selectResourceByIds(@RequestBody List<Integer> resourceIds){
        List<Resource> resources = resourceService.selectResourceByIds(resourceIds);
        return Result.getListResult(Resource.class).setMessage("查询资源集合成功").setData(resources);
    }

    /**
     * 新增管理员资源权限
     * @param form
     * @return
     */
    @PostMapping("/addResource.api")
    @ApiOperation("新增管理员资源权限")
    @YogaLog("新增管理员资源权限")
    @Permission(resources = {"/admin/resource/addResource.api"})
    public Result<String> addResource(@RequestBody AddResourceForm form){
        resourceService.addResource(BeanUtil.toBean(form, AddResourceParam.class));
        return Result.getResult(String.class).setMessage("新增资源成功");
    }

    /**
     * 传入角色ID 分页查询该角色的资源
     * @param form
     * @return
     */
    @PostMapping("/selectResourceByRole.api")
    @ApiOperation("分页查询角色资源")
    @YogaLog("分页查询角色资源")
    @Permission(resources = {"/admin/resource/selectResourceByRole.api"})
    public Result<PageInfo> selectResourceByRole(@RequestBody SelectResourcePageForm form){
        SelectResourcePageParam param = BeanUtil.toBean(form, SelectResourcePageParam.class);
        PageInfo pageInfo = resourceService.selectResourceByRole(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询角色资源成功").setData(pageInfo);
    }

    /**
     * 传入角色ID 资源ID 取消该角色该资源权限
     * @param form
     * @return
     */
    @PostMapping("/cancelResourceRelation.api")
    @ApiOperation("取消资源关联")
    @YogaLog("取消资源关联")
    @Permission(resources = {"/admin/resource/cancelResourceRelation.api"})
    public Result<String> cancelResourceRelation(@Valid @RequestBody CancelResourceRelationForm form){
        CancelResourceRelationParam param = BeanUtil.toBean(form, CancelResourceRelationParam.class);
        resourceService.cancelResourceRelation(param);
        return Result.getResult(String.class).setMessage("取消关联成功");
    }

    /**
     * 传入角色Id 资源ID 使得该角色拥有该资源权限
     * @param form
     * @return
     */
    @PostMapping("/reResourceRelation.api")
    @ApiOperation("资源关联操作")
    @YogaLog("资源关联操作")
    @Permission(resources = {"/admin/resource/reResourceRelation.api"})
    public Result<String> reResourceRelation(@Valid @RequestBody ReResourceRelationForm form){
        ReResourceRelationParam param = BeanUtil.toBean(form, ReResourceRelationParam.class);
        resourceService.ReResourceRelation(param);
        return Result.getResult(String.class).setMessage("关联资源成功");
    }

}

